/**
 * @(#)AbstractTaskManagerActivity.java        1.0 2/26/12
 * 
 * Copyright (c) 2012 public static droid main
 * Georgia Institute of Technology, Atlanta, GA
 * All rights reserved.
 */
package edu.gatech.oad.publicstaticdroidmain.taskmanager.ui;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
/**
 * An Android activity that provide functionality for the other activities
 * 
 * 
 * @version 1.0 Feb 22 2012
 * @author Raheel Merali
 */
public abstract class AbstractTaskManagerActivity extends Activity {

    /**
     * Starts another activity
     * 
     * @param activityName the name of the activity to start
     */
    protected void startUIActivity(String activityName) {
	startUIActivityWithIntent(activityName, new Intent());
    }

    /**
     * Starts the specified activity using the specified Intent
     * 
     * @param activityName the name of the activity to start
     * @param intent the intent to use to launch the activity
     */
    protected void startUIActivityWithIntent(String activityName, Intent intent) {
	String fullyQualifiedName = getString(R.string.ui_package_name) + activityName;

	try {
	    Class<?> classToLaunch = Class.forName(fullyQualifiedName);
	    intent.setClass(getApplicationContext(), classToLaunch);
	    startActivity(intent);
	}
	catch(ClassNotFoundException e) {
	    Log.e(getClass().toString(), "Activity \"" + fullyQualifiedName + "\" was not found.");
	}
    }

    /**
     * Shows a a Toast message using the specified layout.
     * 
     * @param alertType the layout to use
     * @param message the message to display in the Toast
     */
    private void showToast(int alertType, String message) {
	LayoutInflater inflater = getLayoutInflater();
	View layout = inflater.inflate(alertType,
		(ViewGroup) findViewById(R.id.invalid_alert));

	TextView text = (TextView) layout.findViewById(R.id.text);
	text.setText(message);

	Toast toast = new Toast(getApplicationContext());
	toast.setDuration(Toast.LENGTH_LONG);
	toast.setView(layout);
	toast.show();
    }

    public boolean onCreateOptionsMenu(Menu menu)
    {
	MenuInflater inflator = getMenuInflater();
	inflator.inflate(R.menu.menu, menu);
	return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item)
    {
	switch(item.getItemId())
	{
	case R.id.menu_log_out:
	    ((TaskManagerApplication)getApplication()).setTaskManager(null);
	    startUIActivity(getString(R.string.activity_login));
	    break;
	case R.id.menu_manage_categories:
	    startUIActivity(getString(R.string.activity_manage_categories));
	    break;
	}

	return false;
    }
    
    /**
     * Shows a red error toast.
     * 
     * @param message the message to display
     */
    protected void showErrorToast(String message) {
	showToast(R.layout.invalid_alert, message);
    }

    /**
     * Shows a green success toast.
     * 
     * @param message the message to display
     */
    protected void showSuccessToast(String message) {
	showToast(R.layout.valid_alert, message);
    }

}